import { axiosInstance } from 'src/boot/axios'
import qs from 'qs'
import CommonConfig from 'src/assets/js/config'

const upms = CommonConfig.upmsUrl + '/v1'
const datacenterUrl = CommonConfig.datacenterUrl + '/v1'
const planUrl = CommonConfig.planningUrl + '/v1'

const qsConfig = {
  arrayFormat: 'repeat',
  skipNulls: true,
  format: 'RFC3986'
}

/**
 * 列举用户
 * @param pageNum 页码
 * @param pageSize 每页条数
 * @param departmentId 部门 ID
 * @param role 角色
 * @param post 岗位
 * @param status 状态
 * @param searchBy 搜索依据
 * @param keyword 关键字
 * @return {Promise<unknown>} promise
 */
function listUsers (pageNum, pageSize, { departmentId, role, post, searchBy, keyword }) {
  const url = datacenterUrl + '/users'

  let config = {
    params: {
      pageNum,
      pageSize,
      departmentId,
      role: role === 'all' ? null : role,
      post: post === 'all' ? null : post,
      searchBy: searchBy || undefined,
      keyword: keyword || undefined
    }
  }

  return axiosInstance.get(url, config).then(resp => {
    return Promise.resolve(resp.data)
  })
}

export function getUserFilter (params) {
  const url = datacenterUrl + '/users'

  return axiosInstance.get(url, { params }).then(resp => {
    return Promise.resolve(resp)
  })
}
/**
 * 列举用户
 * @param pageNum 页码
 * @param pageSize 每页条数
 * @param departmentId 部门 ID
 * @param role 角色
 * @param post 岗位
 * @param status 状态
 * @param searchBy 搜索依据
 * @param keyword 关键字
 * @return {Promise<unknown>} promise
 */
function listUsersNoPage ({ departmentId, role, post, status, searchBy, keyword }) {
  const url = datacenterUrl + '/users:noPage'

  let config = {
    params: {
      departmentId,
      role,
      post,
      status,
      searchBy: searchBy || undefined,
      keyword: keyword || undefined
    }
  }

  return axiosInstance.get(url, config).then(resp => {
    return Promise.resolve(resp.data)
  })
}

/**
 * 获取部门 tree
 * @return {Promise<unknown>} promise
 */
function getDepartmentsTree () {
  const url = upms + '/department_tree'

  return axiosInstance.get(url).then(resp => {
    return Promise.resolve(resp.data)
  })
}

/**
 * 切换员工状态
 * @param id 员工 ID
 * @param statusCode 状态 code
 * @return {Promise<unknown>} promise
 */
function toggleUserStatus (id, statusCode) {
  const url = datacenterUrl + '/users/' + id

  let reqData = {
    statusCode
  }

  return axiosInstance.patch(url, reqData).then(resp => {
    return Promise.resolve(resp)
  })
}

/**
 * 删除员工
 * @param id {number} 员工 ID
 * @return {Promise<unknown>} promise
 */
function deleteUser (id) {
  const url = datacenterUrl + '/users/' + id

  return axiosInstance.delete(url).then(resp => {
    return Promise.resolve(resp.data)
  })
}

/**
 * 重置用户密码
 * @param id 用户 ID
 * @return {Promise<unknown>} promise
 */
function resetPassword (newPassword, phone) {
  const url = datacenterUrl + `/users/password`

  let reqData = {
    newPassword,
    phone
  }

  return axiosInstance.put(url, reqData).then(resp => {
    return Promise.resolve(resp)
  })
}

/**
 * 创建用户
 * @param number 员工编号
 * @param name 姓名
 * @param genderCode 性别 code
 * @param phone 手机号码
 * @param email E-mail
 * @param icCardNumber IC 卡号
 * @param departmentId 部门 ID
 * @param postCode 岗位 code
 * @param roleIds 角色 ID 数组
 * @param photoUrl 照片 URL
 * @param skillLevelCode 技能水平 code
 * @return {Promise<unknown>} promise
 */
function createUser ({
  number,
  name,
  genderCode,
  phone,
  email,
  icCardNumber,
  departmentId,
  postCode,
  roleIds,
  price,
  photoUrl,
  skillLevelCode
}) {
  const url = datacenterUrl + '/users'

  let reqData = {
    number,
    name,
    genderCode,
    phone,
    email,
    icCardNumber,
    departmentId,
    postCode,
    roleIds,
    price,
    photoUrl,
    skillLevelCode
  }

  return axiosInstance.post(url, reqData).then(resp => {
    return Promise.resolve(resp)
  })
}

/**
 * 编辑用户
 * @param id 用户 ID
 * @param number 员工编号
 * @param name 姓名
 * @param genderCode 性别 code
 * @param phone 手机号码
 * @param email E-mail
 * @param icCardNumber IC 卡号
 * @param departmentId 部门 ID
 * @param postCode 岗位 code
 * @param roleIds 角色 ID 数组
 * @param photoUrl 照片 URL
 * @param skillLevelCode 技能水平 code
 * @param statusCode 用户状态 code
 * @return {Promise<AxiosResponse<any> | unknown>} promise
 */
function editUser (id, {
  number,
  name,
  genderCode,
  phone,
  email,
  icCardNumber,
  departmentId,
  postCode,
  roleIds,
  price,
  photoUrl,
  skillLevelCode,
  statusCode
}) {
  const url = datacenterUrl + '/users/' + id

  let reqData = {
    number,
    name,
    genderCode,
    phone,
    email,
    icCardNumber,
    departmentId,
    postCode,
    roleIds,
    price,
    photoUrl,
    skillLevelCode,
    statusCode
  }

  return axiosInstance.put(url, reqData).then(resp => {
    return Promise.resolve(resp)
  })
}

/**
 * 设置员工工位
 * @param userIds 员工 ID 数组
 * @param workCenterIds {Array<Number>} 工位 ID 数组
 * @returns {Promise<unknown>} promise
 */
function setUsersWorkCenters (userIds, workCenterIds) {
  const url = upms + '/users/work_centers'

  let reqData = {
    userIds,
    workCenterIds
  }

  return axiosInstance.put(url, reqData).then(resp => {
    return Promise.resolve(resp.data)
  })
}

/**
 * 设置员工角色
 * @param userIds 员工 ID 数组
 * @param roleIds {Array<{Number}>} 角色 ID 数组
 * @returns {Promise<unknown>} promise
 */
function setUsersRoles (userIds, roleIds) {
  const url = upms + '/users/roles'

  let reqData = {
    userIds,
    roleIds
  }

  return axiosInstance.put(url, reqData).then(resp => {
    return Promise.resolve(resp.data)
  })
}

/**
 * 列举员工工位
 * @param userIds 员工 ID 数组
 * @returns {Promise<unknown>} promise
 */
function listUsersWorkCenters (userIds) {
  const url = upms + '/users/work_centers'

  let config = {
    params: {
      userIds
    },
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }

  return axiosInstance.get(url, config).then(resp => {
    return Promise.resolve(resp)
  })
}
/**
 * GET
  /v1/users/{icNo}:binding
  根据ic_no获取用户信息
 * @returns {Promise<unknown>} promise
 */
function getUserByIC (userId) {
  const url = datacenterUrl + `/users/${userId}:binding`
  return axiosInstance.get(url).then(resp => {
    return Promise.resolve(resp)
  })
}

/**
 * 批量删除员工
 * @param ids 员工 ID 数组
 * @returns {Promise<unknown>} promise
 */
function batchDeleteUsers (ids) {
  const url = datacenterUrl + '/users'

  let config = {
    params: { ids },
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }

  return axiosInstance.delete(url, config).then(resp => {
    return Promise.resolve(resp)
  })
}

// 增量编辑用户
export function uploadUserPhoto (id, photoUrl) {
  const url = datacenterUrl + '/users/' + id
  let reqData = {
    photoUrl
  }

  return axiosInstance.patch(url, reqData).then(resp => {
    return Promise.resolve(resp)
  })
}

// 获取用户的绑定关系
function getUserBindList (ids) {
  const url = datacenterUrl + `/list_user_bind`
  const config = {
    params: {
      ids
    },
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }
  return axiosInstance.get(url, config).then(res => {
    return Promise.resolve(res.data)
  })
}
function delUserBindList (userId) {
  const url = planUrl + `/delete_user_bind_devices`
  const config = {
    params: {
      userId
    }
  }
  return axiosInstance.delete(url, config).then(res => {
    return Promise.resolve(res)
  })
}

export default {
  listUsers,
  listUsersNoPage,
  getDepartmentsTree,
  toggleUserStatus,
  deleteUser,
  resetPassword,
  createUser,
  editUser,
  setUsersWorkCenters,
  setUsersRoles,
  listUsersWorkCenters,
  batchDeleteUsers,
  uploadUserPhoto,
  getUserByIC,
  getUserBindList,
  delUserBindList
}
